package com.example.himalaya.ui;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.himalaya.R;
import com.example.himalaya.adapters.DetailListAdapter;
import com.example.himalaya.base.BaseActivity;
import com.example.himalaya.interfaces.IAlbumDetailViewCallback;
import com.example.himalaya.interfaces.OnRetryClickLisener;
import com.example.himalaya.presenters.AlbumDetialPresenter;
import com.example.himalaya.presenters.PlayerPresenter;
import com.example.himalaya.utils.ButtonUtils;
import com.example.himalaya.utils.ImageBlur;
import com.example.himalaya.utils.LogUtils;
import com.example.himalaya.views.RoundRectImageView;
import com.example.himalaya.views.UILoader;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import net.lucode.hackware.magicindicator.buildins.UIUtil;

import java.util.List;

public class DetailActivity extends BaseActivity implements IAlbumDetailViewCallback, OnRetryClickLisener, DetailListAdapter.ItemClickListener {

    private String TAG = "DetailActivity";

    private ImageView largeCover;
    private RoundRectImageView smallCover;
    private TextView mAlbumTitle;
    private TextView mAlbumAuthor;
    private AlbumDetialPresenter mAlbumDetialPresenter;
    private RecyclerView mDetailList;
    private int mCurrentPage = 1;
    private DetailListAdapter mDetailListAdapter;
    private FrameLayout mDetailListContainer;
    private UILoader mUiLoader;
    private View mDetailListView;
    private long mCurrentId = -1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        //设置透明
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
        getWindow().setStatusBarColor(Color.TRANSPARENT);

        initView();
        mAlbumDetialPresenter = AlbumDetialPresenter.getsInstance();
        mAlbumDetialPresenter.registerViewCallback(this);
    }

    private void initView() {
        mDetailListContainer = findViewById(R.id.detail_list_container);
        if (mUiLoader == null) {
            mUiLoader = new UILoader(this) {
                @Override
                protected View getSuccessView(ViewGroup container) {
                    return createSuccessView(container);
                }
            };
            mDetailListContainer.removeAllViews();
            mDetailListContainer.addView(mUiLoader);
            mUiLoader.setOnRetryClickListener(DetailActivity.this);
        }
        largeCover = this.findViewById(R.id.iv_large_cover);
        smallCover = this.findViewById(R.id.iv_small_cover);
        mAlbumTitle = this.findViewById(R.id.tv_album_title);
        mAlbumAuthor = this.findViewById(R.id.tv_album_author);


    }

    private View createSuccessView(ViewGroup container) {
        mDetailListView = LayoutInflater.from(this).inflate(R.layout.item_detail_list, container, false);
        mDetailList = mDetailListView.findViewById(R.id.album_detail_list);
        //设置布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        mDetailList.setLayoutManager(layoutManager);
        //设置适配器
        mDetailListAdapter = new DetailListAdapter();
        mDetailList.setAdapter(mDetailListAdapter);
        mDetailList.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                outRect.left = UIUtil.dip2px(view.getContext(), 10);
                outRect.right = UIUtil.dip2px(view.getContext(), 10);
                outRect.bottom = UIUtil.dip2px(view.getContext(), 10);
                // Add top margin only for the first item to avoid double space between items
                if (parent.getChildPosition(view) == 0)
                    outRect.top = UIUtil.dip2px(view.getContext(), 10);

            }
        });
        mDetailListAdapter.setItemClickListener(this);
        return mDetailListView;
    }


    @Override
    public void onDetailListLoaded(List<Track> tracks) {
        //判断数据结果，更具结果控制UI显示
        if (tracks == null || tracks.size() == 0) {
            if (mUiLoader != null) {
                mUiLoader.updateStatus(UILoader.UIStatus.EMPTY);
            }
        }

        if (mUiLoader != null) {
            mUiLoader.updateStatus(UILoader.UIStatus.SUCCESS);
        }
        //更新设置数据
        mDetailListAdapter.setData(tracks);
    }

    @Override
    public void onAlbumLoaded(Album album) {
        long id = album.getId();
        //        LogUtils.d(TAG,"************************"+id );
        mCurrentId = id;
        //获取专辑的详情内容
        mAlbumDetialPresenter.getAlbumDetail((int) id, mCurrentPage);
        //拿数据显示Loading状态
        if (mUiLoader != null) {
            mUiLoader.updateStatus(UILoader.UIStatus.LOADING);
        }


        if (mAlbumTitle != null) {
            mAlbumTitle.setText(album.getAlbumTitle());
        }
        if (mAlbumAuthor != null) {
            mAlbumAuthor.setText(album.getAnnouncer().getNickname());
        }
        if (largeCover != null) {
            //高斯模糊
//            Picasso.with(this).load(album.getCoverUrlLarge()).into(largeCover);
            //异步获取图片
            Picasso.with(this).load(album.getCoverUrlLarge()).into(largeCover, new Callback() {
                @Override
                public void onSuccess() {
                    ImageBlur.makeBlur(largeCover, DetailActivity.this);
                }

                @Override
                public void onError() {
                    LogUtils.d(TAG, "onError!");
                }
            });

        }
        if (smallCover != null) {
            Picasso.with(this).load(album.getCoverUrlLarge()).into(smallCover);
        }
    }

    @Override
    public void onNetWorkError(int i, String s) {
        //请球发生错误显示网络异常
        mUiLoader.updateStatus(UILoader.UIStatus.NETWORK_ERROR);
    }

    @Override
    public void onRetryClick() {
        //表示网络不佳的时候用户点击重试
        //重新获取数据
        if (mAlbumDetialPresenter != null) {
            mAlbumDetialPresenter.getAlbumDetail((int)mCurrentId,mCurrentPage);
        }
    }

    @Override
    public void onItemClick(List<Track> detailData, int position) {
        //TODO:跳转到播放器界面
        //设置播放器的数据
        if (!ButtonUtils.isFastDoubleClick()) {
            PlayerPresenter playerPresenter =PlayerPresenter.getPlayerPresenter();
            playerPresenter.setPlayList(detailData,position);

            Intent intent=new Intent(this,PlayerActivity.class);
            startActivity(intent);

        }

    }
}
